package com.team.v;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.team.m.pojo.OrderInfo;
import com.team.m.pojo.OrderReturnInfo;
import com.team.m.pojo.SignInfo;
import com.team.utils.Configure;
import com.team.utils.HttpRequest;
import com.team.utils.RandomStringGenerator;
import com.team.utils.Signature;
import com.team.utils.StreamUtil;
import com.thoughtworks.xstream.XStream;

/**
 * 支付信息获取控制类
 * 
 * @author kungfupeng
 *
 */
@Controller("payController")
@RequestMapping("/pay")
public class PayController {

	@RequestMapping("/getOpenId")
	@ResponseBody
	public String getOpenId(String code) {
		String openId = "-1";
		try {
			HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/jscode2session?appid=" + Configure.getAppID()
					+ "&secret=" + Configure.getSecret() + "&js_code=" + code + "&grant_type=authorization_code");
			HttpClient httpClient = HttpClients.createDefault();
			HttpResponse res = httpClient.execute(httpGet);
			HttpEntity entity = res.getEntity();
			openId = EntityUtils.toString(entity, "UTF-8");
		} catch (Exception e) {
			openId = "-1";
		}
		return openId;
	}

	@RequestMapping("/getPayResult")
	@ResponseBody
	public String getPayResult(HttpServletRequest request) {
		try {
			String reqParams = StreamUtil.read(request.getInputStream());
			System.out.println(reqParams);
		} catch (Exception e) {
		}
		StringBuffer sb = new StringBuffer("<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>");
		return sb.toString();
	}

	@RequestMapping("/getSign")
	@ResponseBody
	public String getSign(String repay_id) {
		String result = "-1";
		try {
			SignInfo signInfo = new SignInfo();
			signInfo.setAppId(Configure.getAppID());
			long time = System.currentTimeMillis() / 1000;
			signInfo.setTimeStamp(String.valueOf(time));
			signInfo.setNonceStr(RandomStringGenerator.getRandomStringByLength(32));
			signInfo.setRepay_id("prepay_id=" + repay_id);
			signInfo.setSignType("MD5");
			// 生成签名
			String sign = Signature.getSign(signInfo);

			JSONObject json = new JSONObject();
			json.put("timeStamp", signInfo.getTimeStamp());
			json.put("nonceStr", signInfo.getNonceStr());
			json.put("package", signInfo.getRepay_id());
			json.put("signType", signInfo.getSignType());
			json.put("paySign", sign);
			result = json.toJSONString();
		} catch (Exception e) {
			result = "-1";
		}
		return result;
	}

	/**
	 * 下单
	 * 
	 * @return
	 */
	@RequestMapping("/getXD")
	@ResponseBody
	public String getXD(String openid) {
		String result = "-1";
		try {
			OrderInfo order = new OrderInfo();
			order.setAppid(Configure.getAppID());
			order.setMch_id(Configure.getMch_id());
			order.setNonce_str(RandomStringGenerator.getRandomStringByLength(32));
			order.setBody("兰博基尼，意大利的浪漫之选！");
			order.setOut_trade_no(RandomStringGenerator.getRandomStringByLength(32));
			order.setTotal_fee(177799998);
			order.setSpbill_create_ip("http://www.noobkufu.cn");
			order.setNotify_url("http://www.noobkufu.cn");
			order.setTrade_type("购物交易");
			order.setOpenid(openid);
			order.setSign_type("MD5");
			// 生成签名
			String sign = Signature.getSign(order);
			order.setSign(sign);

			String data = HttpRequest.sendPost("https://api.mch.weixin.qq.com/pay/unifiedorder", order);
			XStream xStream = new XStream();
			xStream.alias("xml", OrderReturnInfo.class);

			OrderReturnInfo returnInfo = (OrderReturnInfo) xStream.fromXML(data);
			JSONObject json = new JSONObject();
			json.put("prepay_id", returnInfo.getPrepay_id());
			result = json.toJSONString();
		} catch (Exception e) {
			result = "-1";
		}
		return result;
	}

}
